package com.example.springbott.service;

import com.example.springbott.bean.MarketAd;
import com.example.springbott.bean.MarketIssue;
import com.example.springbott.bean.MarketIssueExample;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.mapper.MarketIssueMapper;
import com.example.springbott.util.StringUtil;
import com.example.springbott.vo.IssueVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.sun.org.glassfish.external.statistics.AverageRangeStatistic;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/21 19:25
 */
@Service
public class AdminIssueServiceIml  implements AdminIssueService{
    @Resource
    MarketIssueMapper marketIssueMapper;

    /**
     * 查找通用问题
     * @param page
     * @param limit
     * @param question
     * @param sort
     * @param order
     * @return
     */
    @Override
    public CommonData<MarketIssue> list(Integer page, Integer limit, String question, String sort, String order) {
        PageHelper.startPage(page,limit);
        MarketIssueExample issueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = issueExample.createCriteria();
        if (!StringUtil.isEmpty(question)){
            criteria.andQuestionLike("%"+question+"%");
        }
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(issueExample);
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(marketIssues);
        int page1 = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit1 = pageInfo.getPageSize();
        long total = pageInfo.getTotal();
        List<MarketIssue> list1 = pageInfo.getList();
        //将查询到的数据进行封装
        CommonData<MarketIssue> data = new CommonData<MarketIssue>(page1,pages,limit1, (int) total,list1);
        return data;
    }


    /**
     * 新增通用问题
     * @param marketIssue
     * @return
     */
    @Override
    public MarketIssue create(MarketIssue marketIssue) {
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        marketIssue.setAddTime(date);
        marketIssue.setUpdateTime(date);
        int i = marketIssueMapper.insertSelective(marketIssue);
        MarketIssue data = marketIssueMapper.selectByPrimaryKey(marketIssue.getId());
        return data;
    }


    /**
     * 修改通用问题
     * @param marketIssue
     * @return
     */
    @Override
    public MarketIssue update(MarketIssue marketIssue) {
        int i = marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        MarketIssue data = marketIssueMapper.selectByPrimaryKey(marketIssue.getId());
        return data;
    }

    @Override
    public void delete(MarketIssue marketIssue) {
        marketIssueMapper.deleteByPrimaryKey(marketIssue.getId());
    }

}
